/**
* @file app_light_tools.c
* @brief Common process - adapter the flash api
* @version 0.1
* @date 2021-10-22
*
* @copyright Copyright 2020-2021 Tuya Inc. All Rights Reserved.
*
*/
#include "app_light_tools.h"

#define TEMP_6500K 500 ///* temp =1000000/attr   500 = 1000000/2000
#define TEMP_2000K 153

/**
 * @note This function shows how to transfor zigbee bright and temperature to app data
 * @param[in] {UCHAR_T} zig_b
 * @param[in] {USHORT_T} zig_t
 * @param[out] {USHORT_T} app_b
 * @param[out] {USHORT_T} app_t
 * @return none
 */
VOID_T app_light_tools_zig_bt_2_app_bt(UCHAR_T zig_b, USHORT_T zig_t, USHORT_T *app_b, USHORT_T *app_t)
{
    if (app_b != NULL)
    {
        *app_b = (USHORT_T)((float)zig_b * APP_WHITE_B_MAX / ZIGBEE_WHITE_B_MAX + 0.5);
    }
    if (app_t != NULL)
    {
        *app_t = (USHORT_T)((float)zig_t * APP_WHITE_T_MAX / ZIGBEE_WHITE_T_MAX + 0.5);
    }
}
/**
 * @note This function shows how to transfor app data to zigbee data
 * @param[in] {USHORT_T} app_b
 * @param[in] {USHORT_T} app_t
 * @param[out] {UCHAR_T} zig_b
 * @param[out] {USHORT_T} zig_t
 * @return none
 */
VOID_T app_light_tools_bt_2_zig_bt(UCHAR_T *zig_b, USHORT_T *zig_t, USHORT_T app_b, USHORT_T app_t)
{
    if (zig_b != NULL)
    {
        *zig_b = (UCHAR_T)((float)app_b * ZIGBEE_WHITE_B_MAX / APP_WHITE_B_MAX + 0.5);
    }

    if (zig_t != NULL)
    {
        *zig_t = (USHORT_T)((float)app_t * ZIGBEE_WHITE_T_MAX / APP_WHITE_T_MAX + 0.5) & 0x00FF;
    }
}
/**
 * @note: ASSIC change to hex
 * @param [in] { UCHAR_T AscCode -> ASSIC code }
 * @return: hex value
 */
UCHAR_T app_light_tools_asc2hex(IN UCHAR_T AscCode)
{
    UCHAR_T ucResult = 0;

    if ('0' <= AscCode && AscCode <= '9')
    {
        ucResult = AscCode - '0';
    }
    else if ('a' <= AscCode && AscCode <= 'f')
    {
        ucResult = AscCode - 'a' + 10;
    }
    else if ('A' <= AscCode && AscCode <= 'F')
    {
        ucResult = AscCode - 'A' + 10;
    }
    else
    {
        ucResult = 0;
    }
    return ucResult;
}
/**
 * @note: four UCHAR_T merge into USHORT_T
 * @param [in] { UCHAR_T HH -> USHORT Hight hight 4bit }
 * @param [in] { UCHAR_T HL -> USHORT Hight low 4bit   }
 * @param [in] { UCHAR_T LH -> USHORT low hight 4bit   }
 * @param [in] { UCHAR_T LL -> USHORT low low 4bit     }
 * @return: USHORT value
 */
USHORT_T app_light_tools_str2short(UCHAR_T HH, UCHAR_T HL, UCHAR_T LH, UCHAR_T LL)
{
    return ((HH << 12) | (HL << 8) | (LH << 4) | (LL & 0x0F));
}
/**
 * @berief: get the max value in 5 numbers
 * @param { ULONG_T a -> number1 }
 * @param { ULONG_T b -> number2 }
 * @param { ULONG_T c -> number3 }
 * @param { ULONG_T d -> number4 }
 * @param { ULONG_T e -> number5 }
 * @return: Max value
 * @retval: none
 */
ULONG_T app_light_tools_get_max_value(ULONG_T a, ULONG_T b, ULONG_T c, ULONG_T d, ULONG_T e)
{
    ULONG_T x = a > b ? a : b;
    ULONG_T y = c > d ? c : d;
    ULONG_T z = x > y ? x : y;

    return (z > e ? z : e);
}
/**
 * @berief: get the absolute value
 * @param { LONG_T value -> calc value }
 * @return: absolute value
 * @retval: none
 */
ULONG_T app_light_tools_get_abs_value(LONG_T value)
{
    return (value > 0 ? value : -value);
}
/**
 * @brief 
 * 
 * @param zig_b 
 * @return USHORT_T 
 */
USHORT_T app_light_tools_zig_b_2_app_B(UCHAR_T zig_b)
{
    USHORT_T result;
    result = (USHORT_T)((float)zig_b * APP_WHITE_B_MAX / ZIGBEE_WHITE_B_MAX + 0.5);
    if (result > APP_WHITE_B_MAX)
    {
        result = APP_WHITE_B_MAX;
    }
    return result;
}
/**
 * @brief 
 * 
 * @param app_B 
 * @return UCHAR_T 
 */
UCHAR_T app_light_tools_B_2_zig_b(USHORT_T app_B)
{
    UCHAR_T result;
    result = (UCHAR_T)((float)app_B * ZIGBEE_WHITE_B_MAX / APP_WHITE_B_MAX + 0.5);
    return result;
}
/**
 * @brief 
 * 
 * @param zigbee_t 
 * @return USHORT_T 
 */
USHORT_T app_light_tools_zig_t_2_app_t(USHORT_T zigbee_t)
{
    USHORT_T app_t = 0;
    if (zigbee_t > TEMP_6500K)
        zigbee_t = TEMP_6500K;
    if (zigbee_t < TEMP_2000K)
        zigbee_t = TEMP_2000K;

    app_t = (USHORT_T)((float)(zigbee_t - TEMP_6500K) * 1000 / (-347) + 0.5); //
    return app_t;
}
/**
 * @brief 
 * 
 * @param app_t 
 * @return USHORT_T 
 */
USHORT_T app_light_tools_t_2_zig_t(USHORT_T app_t)
{
    USHORT_T zigbee_t = 0;
    if (app_t > 1000)
        app_t = 1000;

    zigbee_t = (USHORT_T)((float)500 - ((float)(app_t * 347) / 1000) + 0.5);
    return zigbee_t;
}
